package cn.com.ddapp.ddm.ui.mine;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdos.usbdevice.publicSecurityIDCardLib;
import com.zhy.autolayout.AutoLinearLayout;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.StringUtils;
import cn.com.ddapp.ddm.util.TDevice;

public class CertificationActivity extends BaseActivity {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.name_certify)
    EditText nameCertify;
    @BindView(R.id.idCard_certify)
    EditText idCardCertify;
    @BindView(R.id.commit_certify)
    Button commitCertify;

    @Override
    public void initView() {
        titletext.setText(R.string.nameOk);
    }
    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_certification;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back, R.id.commit_certify})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.commit_certify:
                String userId = SPUtils.getStringData(this, SPUtils.USERID);
                String name = nameCertify.getText().toString();
                String idCard = idCardCertify.getText().toString();
                    if ("".equals(name)) {
                        BaseApplication.getIntstance().showToast("请输入名字", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if ("".equals(idCard)) {
                        BaseApplication.getIntstance().showToast("请输入身份证号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else {
                        saveNameAuth(name,idCard);
                    }
                break;
        }
    }

    private void saveNameAuth(String name, String idCard) {
        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                Gson gson = new Gson();
                Entity entity = gson.fromJson(response, Entity.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                    BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.getIntstance().showToast("实名认证成功", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    finish();
                }
            }
        };
        int ifATM = 0;
        if (TDevice.isTablet(getApplicationContext())) {
            ifATM = 1;//是ATM验证
        } else {
            ifATM = 0;
        }
        OKUtilAPI.post(OKUtilAPI.saveNameAuth, "userId=" + SPUtils.getStringData(this, SPUtils.USERID) + "&name=" + name + "&idcard=" + idCard + "&ifATM=" + ifATM, callback);
    }
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
